local yilu = fk.CreateSkill{
  name = "rfenghou_3d18j__yilu",
  tags = { Skill.Compulsory },
}

yilu:addEffect(fk.TargetSpecified, {
  can_trigger = function (self, event, target, player, data)
    if target == player and data.card and player:hasSkill(yilu.name) and #data:getAllTargets() == 1 then
      local room = player.room
      local mark = player:getMark("rfenghou_3d18j__yilu_record-turn")
      local use_event = room.logic:getMostRecentEvent(GameEvent.UseCard)
      if use_event == nil then return false end
      if mark == 0 then
        room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
          local use = e.data
          if use.card and use.from == player and #use:getAllTargets() == 1 then
            mark = e.id
            room:setPlayerMark(player, "rfenghou_3d18j__yilu_record-turn", mark)
            return true
          end
        end, Player.HistoryTurn)
      end
      return mark == use_event.id
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getAlivePlayers(false), function(p)
      return player:inMyAttackRange(p) and p:getMark("@@rfenghou_3d18j__yilu") == 0
    end)
    if not next(targets) then return end
    local tos = room:askToChoosePlayers(player, {
      targets = targets,
      min_num = 1,
      max_num = 1,
      skill_name = yilu.name,
      prompt = "#rfenghou_3d18j__yilu-invoke:::" .. data.card:toLogString(),
      cancelable = false,
    })
    if tos[1] then
      event:setCostData(self, { tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local to = dat.tos[1]
    room:addPlayerMark(to, "@@rfenghou_3d18j__yilu")
    local extra_targets = data:getExtraTargets()
    if extra_targets[1] then
      local new_target = room:askToChoosePlayers(to, {
        targets = extra_targets,
        min_num = 1,
        max_num = 1,
        skill_name = yilu.name,
        prompt = "#rfenghou_3d18j__yilu-extra:" .. player.id .. "::" .. data.card:toLogString(),
        cancelable = false,
      })[1]
      data:addTarget(new_target)
    end

    room:addPlayerMark(player, "@rfenghou_3d18j__yilu_minus")
  end
})
yilu:addEffect("atkrange", {
  correct_func = function(self, from, to)
    local mark = from:getMark("@rfenghou_3d18j__yilu_minus")
    if mark ~= 0 then
      return -mark
    end
  end,
  final_func = function(self, player)
    local before = player:getAttackRange(nil, { self.name })
    if player:getMark("@rfenghou_3d18j__yilu_minus") >= before then
      return 0
    end
  end
})
yilu:addEffect("atkrange", {
  within_func = function(self, from, to)
    if not from:hasSkill(yilu.name) then return false end
    if table.every(Fk:currentRoom().alive_players, function(top)
      return not from:inMyAttackRange(top, nil, nil, { self.name })
    end) then
      return true
    end
  end,
})

Fk:loadTranslationTable{
  ["rfenghou_3d18j__yilu"] = "倚鹭",
  [":rfenghou_3d18j__yilu"] = "锁定技，你每回合首次使用牌指定唯一目标后，你选择攻击范围内一名未以此法选择过的角色，令其为之增加一名合法目标，然后你的攻击范围-1（可减至0）。若你的攻击范围内没有角色，视为包含所有角色。",

  ["#rfenghou_3d18j__yilu-invoke"] = "倚鹭：你须令一名角色为 %arg 增加一名合法目标",
  ["#rfenghou_3d18j__yilu-extra"] = "倚鹭：你须为 %src 使用的 %arg 增加一名合法目标",

  ["@@rfenghou_3d18j__yilu"] = "倚鹭",
  ["@rfenghou_3d18j__yilu_minus"] = "倚鹭－",
}

return yilu
